<?php
class OrdersController extends AppController {

	var $name = 'Orders';
	var $helpers = array('Html', 'Form', 'Time');
	var $components = array( 'RequestHandler', 'Email' );

	function index() {
		$this->Order->recursive = 0;
		$this->redirect('/');
		$this->set('orders', $this->paginate());
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid Order.', true));
			$this->redirect(array('/'));
		}
		$this->set('order', $this->Order->read(null, $id));
	}

	function add() {
		
		if (!empty($this->data)) {
			$this->Order->create();
			if ($this->Order->save($this->data)) {
				$this->Session->setFlash(__('The Order has been saved', true));
				$id = $this->Order->getInsertID();
				
				$this->_sendmail($id, Configure::read('Mail.subject'), Configure::read('Mail.body'), true);
				
				$this->redirect(array('action'=>'view', $id));
			} else {
				$this->Session->setFlash(__('The Order could not be saved. Please, try again.', true));
			}
		}
		//App::import('Sanitize');
		//$data['Tip']['name'] = Sanitize::html($data['Tip']['name'], true);
		if($this->RequestHandler->isAjax()) Configure::write ('debug', 0);
		//$this->layout = 'ajax';
	}
	
	function _sendmail($id, $subject, $message, $informadmin = true) {
		$order = $this->Order->read(null, $id);
		// Send email to user
		App::import("Helper", "Html");
	    $html = new HtmlHelper();
		$url = $html->url(array('admin'=> false, 'controller'=>'orders', 'action'=>'view', $id), true);
		
		$mail_settings = Configure::read('Mail');
		$this->Email->from = $mail_settings['from_name'] . ' <' . $mail_settings['from_mail'] . '>';
		$this->Email->to = $order['Order']['email'];
		if($informadmin) {
			$this->Email->cc = array($mail_settings['admin_mail']);
		}
		$this->Email->subject = $subject;
		$body = $message;
		$body = $body . "\n" . $url;
		$this->Email->send($body);
	}

	function admin_index() {
		$this->Order->recursive = 0;
		$this->set('orders', $this->paginate());
	}

	function admin_view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid Order.', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->set('order', $this->Order->read(null, $id));
	}

	function admin_add() {
		if (!empty($this->data)) {
			$this->Order->create();
			if ($this->Order->save($this->data)) {
				$this->Session->setFlash(__('The Order has been saved', true));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('The Order could not be saved. Please, try again.', true));
			}
		}
	}

	function admin_edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid Order', true));
			$this->redirect(array('action'=>'index'));
		}
		if (!empty($this->data)) {
			$old_order = $this->Order->read(null, $id);
			if ($this->Order->save($this->data)) {
				
				if($this->data['Order']['paid'] == 1 && $old_order['Order']['paid'] == 0) {
					$this->_sendmail($id, Configure::read('Mail.subject_paid'), Configure::read('Mail.body_paid'), false);
					$this->Session->setFlash(__('The Order has been saved and an email has been sent to user', true));
				} else {
					$this->Session->setFlash(__('The Order has been saved', true));
				}
				
				
				
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('The Order could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Order->read(null, $id);
		}
	}

	function admin_delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for Order', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Order->del($id)) {
			$this->Session->setFlash(__('Order deleted', true));
			$this->redirect(array('action'=>'index'));
		} else {
			$this->Session->setFlash(__('Order deleted', true));
			$this->redirect(array('action'=>'index'));
		}
	}

}
?>